package cn.itsource.crm.service.impl;

import cn.itsource.crm.domain.Employee;
import cn.itsource.crm.domain.Tenant;
import cn.itsource.crm.mapper.TenantMapper;
import cn.itsource.crm.query.BaseQuery;
import cn.itsource.crm.service.IEmployeeService;
import cn.itsource.crm.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TenantServiceImpl extends BaseServiceImpl<Tenant> implements ITenantService {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private TenantMapper tenantMapper;

    @Override
    @Transactional
    public void save(Tenant tenant) {
        /*创建时间*/
        Date date = new Date();
        tenant.setRegisterTime(date);
        /*设置状态 默认启用*/
        tenant.setState(true);
        /*先保存租户 ,返回租户id*/
        super.save(tenant);

        /*把租户保存到employee中,后面租户使用该employee账号管理系统*/
        Employee employee = new Employee();
        employee.setEmail(tenant.getEmail());
        employee.setInputTime(date);
        employee.setPassword(tenant.getPassword());
        employee.setState(true);
        employee.setTel(tenant.getCompanyNum());
        employee.setRealName(tenant.getCompanyName());
        employee.setTenant(tenant);
        employee.setUsername("租户管理员");
        employeeService.save(employee);
    }

    @Override
    public Tenant queryByName(BaseQuery query) {
        Tenant tenant = tenantMapper.queryByName(query);
        return tenant;
    }
}
